package io.renren.modules.sys.back.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.back.entity.BtCustomerBaseEntity;
import io.renren.modules.sys.back.entity.BtCustomerProBaseinfoEntity;
import io.renren.modules.sys.back.entity.BtNavBarEntity;
import io.renren.modules.sys.back.service.BtCustomerBaseService;
import io.renren.modules.sys.back.service.BtCustomerProBaseinfoService;
import io.renren.modules.sys.back.service.BtNavBarService;
import io.renren.modules.sys.back.vo.CustomersVo;
import io.renren.modules.sys.back.vo.NavBarCVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author goudandan
 * @email reszj1@gmail.com
 * @date 2018-09-12 16:49:25
 */
@RestController
@RequestMapping("sys/btnavbar")
public class BtNavBarController {
    @Autowired
    private BtNavBarService btNavBarService;

    /**
     *获取产品分类树
     */
    @RequestMapping("/getcategoryztree")
    @RequiresPermissions("sys:btnavbar:save")
    public R GetPCZtree(){
        List<NavBarCVo> pcztree = btNavBarService.getCategroyZtree();
        NavBarCVo dd = new NavBarCVo();
        dd.setId((long)(0));
        dd.setPid((long)(-1));
        dd.setName("全部导航栏分类");
        pcztree.add(dd);
        return R.ok().put("categroyztree",pcztree);
    }
    @RequestMapping("/addormodcategroy")
    @RequiresPermissions("sys:btnavbar:save")
    public R AddorModPC(@RequestBody NavBarCVo categvo){
        if(null != categvo.getId()){
            BtNavBarEntity modbean = btNavBarService.selectById(categvo.getId());
            if(null != modbean){
                modbean.setName(categvo.getName());
                btNavBarService.updateById(modbean);
                return R.ok();
            }else{
                return R.error("无修改的分类");
            }
        }else{
            BtNavBarEntity addbean = new BtNavBarEntity();
            addbean.setName(categvo.getName());
            addbean.setPid(categvo.getPid());
            btNavBarService.insert(addbean);
            return R.ok();
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delcategroy/{id}")
    @RequiresPermissions("sys:btnavbar:save")
    public R delProCateg(@PathVariable("id") Long id){
        btNavBarService.deleteById(id);
        return R.ok();
    }
    /**
     * 能否删除子分类
     */
    @RequestMapping("/iscandelete/{id}")
    public R IsCanDelete(@PathVariable("id") Long id){
        Integer count= btNavBarService.hadSubCount(id);
        if(count>0){
            return R.ok();
        }else{
            return R.error();
        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:btnavbar:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = btNavBarService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:btnavbar:info")
    public R info(@PathVariable("id") Long id){
        BtNavBarEntity btNavBar = btNavBarService.selectById(id);

        return R.ok().put("btNavBar", btNavBar);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:btnavbar:save")
    public R save(@RequestBody BtNavBarEntity btNavBar){
        btNavBarService.insert(btNavBar);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:btnavbar:update")
    public R update(@RequestBody BtNavBarEntity btNavBar){
        ValidatorUtils.validateEntity(btNavBar);
        btNavBarService.updateAllColumnById(btNavBar);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:btnavbar:delete")
    public R delete(@RequestBody Long[] ids){
        btNavBarService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }


    //商铺
    @Autowired
    private BtCustomerBaseService btCustomerBaseService;
    @RequestMapping("/getcustomersztree")
    public R GetCustomersZtree(){
        List<BtCustomerBaseEntity> custlist = btCustomerBaseService.selectList(new EntityWrapper<BtCustomerBaseEntity>());
        CustomersVo dd = new CustomersVo();
        dd.setId((long)(0));
        dd.setPid((long)(-1));
        dd.setName("全部商铺");
        List<CustomersVo> pcztree = new ArrayList<>();
        pcztree.add(dd);
        for(BtCustomerBaseEntity cl:custlist){
            CustomersVo tdd = new CustomersVo();
            tdd.setId(cl.getId());
            tdd.setImg(cl.getMainimg());
            tdd.setName(cl.getName());
            tdd.setPid(0L);
            pcztree.add(tdd);
        }

        return R.ok().put("categroyztree",pcztree);
    }
}
